package net.sxnic.item.ajax;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sxnic.item.ItemActionSupport;
import net.sxnic.jhitem.subject.Subject;
import net.sxnic.jhitem.subject.SubjectManager;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class SelSubject extends ItemActionSupport {

	private static final long serialVersionUID = 1L;

	private static Logger logger = LoggerFactory.getLogger(SelRegion.class);

	@Autowired
	SubjectManager subjectManager;

	public String execute() throws Exception {
		String key = request.getParameter("key");

		Map<String, String> map = new LinkedHashMap<String, String>();

		if ("0".equals(key)) {
			List<Subject> topLevel = subjectManager.getTopLevels();
			for (Subject subject : topLevel) {
				map.put(subject.getSubjectCode(), subject.getSubjectName());
			}
		}

		if (key.length() == 5) {
			List<Subject> subLevel2 = subjectManager.getSubLevel2(StringUtils
					.right(key, 3));
			for (Subject subject : subLevel2) {
				map.put(subject.getSubjectCode(), subject.getSubjectName());
			}
		}

		if (key.length() == 12) {
			List<Subject> subLevel3 = subjectManager.getSubLevel3(StringUtils
					.right(key, 6));
			for (Subject subject : subLevel3) {
				map.put(subject.getSubjectCode(), subject.getSubjectName());
			}
		}

		return "";
		// return writeJson(map);
	}

}
